package vashion.azeroth.core.api.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;

import org.springframework.stereotype.Service;

import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.core.api.request.AddCompletedRequest;
import vashion.azeroth.core.api.request.AddOrderRequest;
import vashion.azeroth.core.api.request.Detail;
import vashion.azeroth.core.api.request.Invoice;
import vashion.azeroth.core.api.request.Payment;
import vashion.azeroth.core.api.request.UpdateOrderMemoRequest;
import vashion.azeroth.core.api.response.AddCompletedResponse;
import vashion.azeroth.core.api.response.AddOrderResponse;
import vashion.azeroth.core.api.response.UpdateOrderMemoResponse;
import vashion.azeroth.core.api.rest.HttpRestRequest;
import vashion.azeroth.core.api.service.ApiOrderService;
import vashion.azeroth.crm.constant.CrmUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;

@Service
public class ApiOrderServiceImpl implements ApiOrderService {

	public AddOrderResponse buidErrorAddOrderResponse(String errorCode,
			String errorDesc) {

		AddOrderResponse aor = new AddOrderResponse();

		aor.setSuccess(false);

		aor.setErrorCode(errorCode);

		aor.setErrorDesc(errorDesc);

		return aor;
	}

	public AddCompletedResponse buidErrorAddCompletedResponse(String errorCode,
			String errorDesc) {

		AddCompletedResponse aor = new AddCompletedResponse();

		aor.setSuccess(false);

		aor.setErrorCode(errorCode);

		aor.setErrorDesc(errorDesc);

		return aor;
	}

	@Override
	public AddOrderResponse addOrder(AddOrderRequest addOrderRequest) {

		if (!addOrderRequest.check()) {

			return buidErrorAddOrderResponse(
					GenericErrorEnum.PARAMETER_ERROR.getErrorMessage(),
					GenericErrorEnum.PARAMETER_ERROR.getErrorMessage());
		}

		try {
			// 将参数转换为JSON格式，用于REST请求
			String request = addOrderRequest.buildJson();

			// 通过HTTP请求
			String res = HttpRestRequest.exec(request);

			// 将返回参数转换为对象
			AddOrderResponse aor = JSON
					.parseObject(res, AddOrderResponse.class);

			return aor;

		} catch (Exception e) {

			return buidErrorAddOrderResponse(
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());

		}

	}

	public AddCompletedResponse addCompletedOrder(
			AddCompletedRequest addCompletedRequest) {

		if (!addCompletedRequest.check()) {

			return buidErrorAddCompletedResponse(
					GenericErrorEnum.PARAMETER_ERROR.getErrorMessage(),
					GenericErrorEnum.PARAMETER_ERROR.getErrorMessage());
		}

		try {
			// 将参数转换为JSON格式，用于REST请求
			String request = addCompletedRequest.buildJson();

			// 通过HTTP请求
			String res = HttpRestRequest.exec(request);

			// 将返回参数转换为对象
			AddCompletedResponse acr = JSON.parseObject(res,
					AddCompletedResponse.class);

			return acr;

		} catch (Exception e) {

			return buidErrorAddCompletedResponse(
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());

		}

	}

	public UpdateOrderMemoResponse updateOrderMemo(
			UpdateOrderMemoRequest updateOrderMemoRequest) {

		if (!updateOrderMemoRequest.check()) {

			UpdateOrderMemoResponse u = new UpdateOrderMemoResponse();

			u.setSuccess(false);
			u.setErrorCode(GenericErrorEnum.PARAMETER_ERROR.getErrorMessage());
			u.setErrorDesc(GenericErrorEnum.PARAMETER_ERROR.getErrorMessage());

			return u;
		}

		try {
			// 将参数转换为JSON格式，用于REST请求
			String request = updateOrderMemoRequest.buildJson();

			// 通过HTTP请求
			String res = HttpRestRequest.exec(request);

			// 将返回参数转换为对象
			UpdateOrderMemoResponse acr = JSON.parseObject(res,
					UpdateOrderMemoResponse.class);

			return acr;

		} catch (Exception e) {

			UpdateOrderMemoResponse u = new UpdateOrderMemoResponse();

			u.setSuccess(false);
			u.setErrorCode(GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
			u.setErrorDesc(GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());

			return u;

		}
	}

	public static void main(String[] args) {

		ApiOrderService apiOrderService = new ApiOrderServiceImpl();

		AddOrderRequest addOrderRequest = new AddOrderRequest();

		/**
		 * 要加一下电话
		 * 
		 * */
		addOrderRequest.setReceiverMobile("11223344");

		/**
		 * 店铺code
		 * */

		@NonNull
		String shopCode = "taobao";

		addOrderRequest.setShopCode(shopCode);

		/**
		 * 物流公司code
		 * */

		@NonNull
		String expressCode = "EMS";

		addOrderRequest.setExpressCode(expressCode);

		/**
		 * 仓库code
		 * */

		@NonNull
		String warehouseCode = "01";

		addOrderRequest.setWarehouseCode(warehouseCode);

		/**
		 * 会员code
		 * */

		@NonNull
		String vipCode = "vip code test";

		addOrderRequest.setVipCode(vipCode);

		/**
		 * 拍单时间
		 * 
		 * 例：2015-03-17 15:03:45
		 * */

		@NonNull
		String dealDatetime = CrmUtil.convertDateToString(new Date());

		addOrderRequest.setDealDatetime(dealDatetime);

		/**
		 * 发票类型 1-普通发票；2-增值发票
		 * 
		 * */

		@NonNull
		int invoiceType = 2;

		addOrderRequest.setInvoiceType(invoiceType);

		/**
		 * details商品信息数组
		 * 
		 * */

		@NonNull
		List<Detail> details = new ArrayList<Detail>();

		Detail detail = new Detail();

		detail.setItemCode("item code test");

		details.add(detail);

		addOrderRequest.setDetails(details);

		/**
		 * payments支付信息数组
		 * 
		 * */

		@NonNull
		List<Payment> payments = new ArrayList<Payment>();

		Payment paymentObj = new Payment();

		/**
		 * 支付类型code
		 * 
		 * */

		@NonNull
		String payTypeCode = "pay type code test";

		paymentObj.setPayTypeCode(payTypeCode);

		/**
		 * 支付金额
		 * 
		 * */

		@NonNull
		Double payment = 12.3D;

		paymentObj.setPayment(payment);

		payments.add(paymentObj);

		addOrderRequest.setPayments(payments);

		/**
		 * invoices发票信息数组
		 * 
		 * */

		@NonNull
		List<Invoice> invoices = new ArrayList<Invoice>();

		Invoice invoice = new Invoice();

		/**
		 * 发票类型
		 * 
		 * */

		@NonNull
		int invoiceType1 = 2;

		invoice.setInvoiceType(invoiceType1);

		invoices.add(invoice);

		addOrderRequest.setInvoices(invoices);

		AddOrderResponse response = apiOrderService.addOrder(addOrderRequest);

		System.out.println(JSON.toJSONString(response, true));

	}

}
